using Commons.Injector;
using Commons.Models.Settings.Matchers.Operators;
using Commons.Utils.Extractors;
using System.Collections;
using System.Collections.Generic;
using System;

namespace Commons.Models.Settings.Matchers
{
	[Serializable]
	public class DataExtractorRule : IRule<object>
	{
		[NonSerialized]
		public static IDataExtractor dataExtractor = DataExtractor.GetDefault();

		private IRelationalOperator<object> relationalOperator;
		public ValuePath ValuePath {get; private set;}
		public object ControlValue {get; private set;}

		public DataExtractorRule(IRelationalOperator<object> relationalOperator, ValuePath valuePath, object controlValue)
		{
			this.relationalOperator = relationalOperator;
			this.ValuePath = valuePath;
			this.ControlValue = controlValue;
		}

		public bool Met(object target)
		{
			object actualValue = dataExtractor.GetValue(ValuePath, target);
			return relationalOperator.Operate(actualValue, ControlValue);
		}

		public override string ToString()
		{
			return string.Format("[DataExtractorRule {0} {1}]", ValuePath, relationalOperator.Type.Name);
		}
	}
}

